import Vue from "vue"
import routeDict from "@/dict/route"
module.exports = {
  provide() {
    return {
      usePage: () => this,
      toView: this.$toView
    }
  },
  created() {
    this._handleEvent()
    this._handleSetFromRouter()
    this._handleSetCurrentRouter()
    this._handleSetShareMenu()
  },
  onUnload() {
    uni.$emit("onUnload")
    this._handleSetLeaveRouter()
  },
  onPageScroll(e) {
    uni.$emit("onPageScroll", e)
  },
  onReachBottom() {
    uni.$emit("onReachBottom")
  },
  onPullDownRefresh() {
    uni.$emit("onPullDownRefresh")
  },
  // onBackPress(e) {
  //   uni.$emit("onBackPress", e)
  // },
  onShareAppMessage() {
    return {
      path: this._shareInfo.path,
      title: this._shareInfo.title || this.$store.getters.indexDeco.config.shareTitle,
      imageUrl: this._shareInfo.imageUrl || this.$store.getters.indexDeco.config.shareImage
    }
  },
  methods: {
    _handleSetShareMenu() {
      // #ifdef MP-WEIXIN
      const routeTtem = routeDict.find((item) => item.route == uni.$currentRoute)
      !routeTtem?.share && uni.hideShareMenu()
      // #endif
    },
    _handleSetCurrentRouter() {
      const routes = getCurrentPages()
      const router = routes[routes.length - 1]
      if (router) {
        Vue.prototype.$currentRoute = router.route
        uni.$currentRoute = router.route
      }
    },
    _handleSetFromRouter() {
      const routes = getCurrentPages()
      const router = routes[routes.length - 2]
      if (router) {
        Vue.prototype.$fromRoute = router.route
        uni.$fromRoute = router.route
      }
    },
    _handleSetLeaveRouter() {
      const routes = getCurrentPages()
      const router = routes[routes.length - 1]
      if (router) {
        // #ifdef WEB
        Vue.prototype.$leaveRoute = this.route
        uni.$leaveRoute = this.route
        // #endif
        // #ifndef WEB
        Vue.prototype.$leaveRoute = router.route
        uni.$leaveRoute = router.route
        // #endif
      }
    },
    _handleEvent() {
      // #ifdef WEB
      window.addEventListener("popstate", (e) => {
        uni.$emit("popstate", e)
      })
      // #endif
    }
  }
}
